<template>
  <div class="com_container">
    <div class="com_chart" ref="ref_rank"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      echartInstance: null,
      allData: [],
      titleSize: 0,
      // 开始的值
      startValue: 0,
      endValue: 9,
      // 定时器标识
      timerId: null
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      console.log('主题改变了')
      console.log(this.theme)

      this.echartInstance.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.upData() // 更新图表的展示
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
    this.startInterval()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timerId)
  },
  methods: {
    async initChart() {
      this.echartInstance = this.$echarts.init(this.$refs.ref_rank, this.theme)

      const initOption = {
        title: {
          text: '▎ 地区销售排行',
          left: 20,
          top: 20
        },
        tooltip: {
          show: true
        },
        grid: {
          top: '30%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            type: 'bar'
          }
        ]
      }
      this.echartInstance.setOption(initOption)

      this.echartInstance.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.echartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    async getData() {
      const { data: res } = await this.$http.get('rank')
      console.log(res)
      this.allData = res
      // 将数据从大到小渲染
      this.allData.sort((a, b) => {
        return b.value - a.value
      })
      this.upData()
    },
    upData() {
      const colorArr = [
        ['#0BA82C', '#4FF778'],
        ['#2E72BF', '#23E5E5'],
        ['#5052EE', '#AB6EE5']
      ]
      // 处理数据
      const provienceArr = this.allData.map((i) => {
        return i.name
      })
      const seriseArr = this.allData.map((i) => {
        return i.value
      })

      const dataOption = {
        xAxis: {
          data: provienceArr
        },
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue
        },
        series: [
          {
            data: seriseArr,
            itemStyle: {
              color: (i) => {
                let targetColorArr = null
                // eslint-disable-line no-unused-vars
                if (i.value > 300) {
                  targetColorArr = colorArr[0]
                } else if (i.value > 200) {
                  targetColorArr = colorArr[1]
                } else {
                  targetColorArr = colorArr[2]
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0]
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1]
                  }
                ])
              }
            }
          }
        ]
      }
      this.echartInstance.setOption(dataOption)
    },
    screenAdapter() {
      this.titleSize = (this.$refs.ref_rank.offsetWidth / 100) * 3.6
      const screenOption = {
        title: {
          textStyle: {
            fontSize: this.titleSize
          }
        },
        series: {
          // 柱形宽度
          barWidth: this.titleSize,
          itemStyle: {
            // 圆角
            barBorderRadius: [this.titleSize / 2, this.titleSize / 2, 0, 0]
          }
        }
      }
      this.echartInstance.setOption(screenOption)
      this.echartInstance.resize()
    },
    // 定时器调节区域缩放
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.startValue++
        this.endValue++
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0
          this.endValue = 9
        }
        this.upData()
      }, 2000)
    }
  }
}
</script>

<style lang="scss" scoped></style>
